package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.PickingReturnMapper;
import com.kang.pojo.Picking;
import com.kang.pojo.PickingItem;
import com.kang.pojo.PickingReturn;
import com.kang.service.PickingReturnService;
import com.vonechina.entity.page.PickingPage;
import com.vonechina.entity.page.PickingReturnPage;

/**
 * 领料归还服务
 * 
 * @author ysq
 *
 */
@Service
public class PickingReturnServiceImpl implements PickingReturnService {
	/**
	 * 领料归还
	 */
	@Resource
	private PickingReturnMapper pickingReturnMapper;

	/**
	 * 查询领料归还
	 */
	@Override
	public List<PickingReturn> selectByWheres(
			PickingReturnPage pickingReturnPage) {
		// 查询领料归还总条数
		pickingReturnPage.setRows(pickingReturnMapper
				.selectByWheresSum(pickingReturnPage));
		return pickingReturnMapper.selectByWheres(pickingReturnPage);
	}

	/**
	 * 查询领料出库单-用于选择领料出库单
	 */
	@Override
	public List<Picking> selectPickingList(PickingPage pickingPage) {
		// 查询领料出库单总条数
		pickingPage.setRows(pickingReturnMapper
				.selectPickingListSum(pickingPage));
		return pickingReturnMapper.selectPickingList(pickingPage);
	}

	/**
	 * 根据pickingID查询领料出库单
	 */
	@Override
	public Picking selectPickingByPickingID(Integer pickingID) {
		return pickingReturnMapper.selectPickingByPickingID(pickingID);
	}

	/**
	 * 根据pickingID查询出picking_iteminfomation（领料出库-库位-物品（表））
	 */
	@Override
	public List<PickingItem> selectPickingItemByPickingID(Integer pickingID) {
		return pickingReturnMapper.selectPickingItemByPickingID(pickingID);
	}

	/**
	 * 添加领料归还信息
	 */
	@Override
	public int addPickingReturn(PickingReturn pickingReturn) {
		return pickingReturnMapper.addPickingReturn(pickingReturn);
	}

	/**
	 * 生成领料归还单号
	 */
	@Override
	public String generatePickingreturnNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = pickingReturnMapper.selectMaxPickingreturnNumber();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "GH" + no;
	}

	/**
	 * 根据pickingreturnID查询领料归还
	 */
	@Override
	public PickingReturn selectPickingReturnByPickingreturnID(
			int pickingreturnID) {
		return pickingReturnMapper
				.selectPickingReturnByPickingreturnID(pickingreturnID);
	}

	/**
	 * 修改领料归还信息
	 */
	@Override
	public int updatePickingReturn(PickingReturn pickingReturn) {
		return pickingReturnMapper.updatePickingReturn(pickingReturn);
	}

	/**
	 * 根据pickingreturnID删除pickingreturn
	 */
	@Override
	public int deletePickingreturn(Integer pickingreturnID) {
		return pickingReturnMapper.deletePickingreturn(pickingreturnID);
	}
}
